package com.dundun.SSO.manage.impl;

import com.dundun.SSO.core.impl.BaseServiceImpl;
import com.dundun.SSO.manage.ISsoGroupService;
import com.dundun.SSO.manage.ISsoUserGroupRelationService;
import com.dundun.SSO.manage.ISsoUserRoleRelationService;
import com.dundun.SSO.manage.dao.SsoGroupDao;
import com.dundun.SSO.manage.dao.SsoUserGroupRelationDao;
import com.dundun.SSO.manage.pojo.SsoGroup;
import com.dundun.SSO.manage.pojo.SsoUser;
import com.dundun.SSO.manage.pojo.SsoUserGroupRelation;
import com.dundun.SSO.manage.query.SsoGroupQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/5 0005 16:17
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Service
public class SsoGroupServiceImpl  extends BaseServiceImpl<SsoGroup> implements ISsoGroupService {
    @Autowired
    private SsoGroupDao ssoGroupDao;

    @Autowired
    private SsoUserGroupRelationDao ssoUserGroupRelationDao;

    @Autowired
    private ISsoUserGroupRelationService ssoUserGroupRelationService;

    public List<SsoGroup> selectByPage(SsoGroupQuery ssoGroupQuery) {
        return ssoGroupDao.selectByPage(ssoGroupQuery);
    }

    public Integer selectCountByPage(SsoGroupQuery ssoGroupQuery) {
        return ssoGroupDao.selectCountByPage(ssoGroupQuery);
    }

    public List<SsoGroup> selectAllParentGroup(Long id) {
        List<SsoGroup> ssoGroupList = new ArrayList<SsoGroup>();
        do{
            SsoGroup ssoGroup = ssoGroupDao.selectByPrimaryKey(id);
            if(null == ssoGroup){
                break;
            }
            ssoGroupList.add(ssoGroup);
            id = ssoGroup.getParentId();
        }while (id != null);
        return ssoGroupList;
    }

    public List<SsoGroup> selectAllSubgroup(Long id) {

        return null;
    }

    public List<SsoGroup> selectAllGroupByUserId(Long userId) {
        List<SsoUserGroupRelation> ssoUserGroupRelationList = ssoUserGroupRelationService.selectAllUserGroupRelationByUserId(userId);
        List<SsoGroup> ssoGroupList = new ArrayList<SsoGroup>();
        for(SsoUserGroupRelation ssoUserGroupRelation:ssoUserGroupRelationList){
            ssoGroupList.add(ssoGroupDao.selectByPrimaryKey(ssoUserGroupRelation.getGroupId()));
            ssoGroupList.addAll(selectAllParentGroup(ssoUserGroupRelation.getGroupId()));
        }
        return ssoGroupList;
    }

    public boolean deleteList(List<Long> idList) {
        return false;
    }
}
